package com.hrp.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.sql.SQLException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * Created by Administrator on 2018/8/1 0001.
 */



@Configuration
@AutoConfigureAfter(MybatisAutoConfiguration.class)
@MapperScan(basePackages = {RadmDataSourceConfig.DAOPACKAGE}, sqlSessionFactoryRef = "radmSqlSessionFactory")
public class RadmDataSourceConfig {


    private Logger logger = LoggerFactory.getLogger(RadmDataSourceConfig.class);


    static final String DAOPACKAGE = "com.hrp.*.hrpdao";
    //static final String MAPPACKAGE = "com.hrp.*.radmmapper";
    //static final String MAPPER_LOCATION = "classpath:com.hrp.*.radmmapper/*.xml";

    @Value("${radm.datasource.url}")
    private String url;

    @Value("${radm.datasource.username}")
    private String user;

    @Value("${radm.datasource.password}")
    private String password;

    @Value("${radm.datasource.driverClassName}")
    private String driverClass;


    @Value("${spring.datasource.initialSize}")
    private int initialSize;

    @Value("${spring.datasource.minIdle}")
    private int minIdle;

    @Value("${spring.datasource.maxActive}")
    private int maxActive;

    @Value("${spring.datasource.maxWait}")
    private int maxWait;

    @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
    private int timeBetweenEvictionRunsMillis;

    @Value("${spring.datasource.minEvictableIdleTimeMillis}")
    private int minEvictableIdleTimeMillis;

    @Value("${spring.datasource.validationQuery}")
    private String validationQuery;

    @Value("${spring.datasource.testWhileIdle}")
    private boolean testWhileIdle;

    @Value("${spring.datasource.testOnBorrow}")
    private boolean testOnBorrow;

    @Value("${spring.datasource.testOnReturn}")
    private boolean testOnReturn;

    @Value("${spring.datasource.filters}")
    private String filters;

    @Value("${spring.datasource.logSlowSql}")
    private String logSlowSql;




    @Bean(name = "radmDataSource")
    @Primary//设置其为默认数据源
    public DataSource radmDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);


        dataSource.setInitialSize(initialSize);
        dataSource.setMinIdle(minIdle);
        dataSource.setMaxActive(maxActive);
        dataSource.setMaxWait(maxWait);
        dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        dataSource.setValidationQuery(validationQuery);
        dataSource.setTestWhileIdle(testWhileIdle);
        dataSource.setTestOnBorrow(testOnBorrow);
        dataSource.setTestOnReturn(testOnReturn);
        try {
            dataSource.setFilters(filters);
        } catch (SQLException e) {
            logger.error("druid configuration initialization filter", e);
        }

        return dataSource;
    }

    @Bean(name = "radmTransactionManager")
    @Primary
    public DataSourceTransactionManager radmTransactionManager() {
        return new DataSourceTransactionManager(radmDataSource());
    }

    @Bean(name = "radmSqlSessionFactory")
    @Primary
    public SqlSessionFactory radmSqlSessionFactory(@Qualifier("radmDataSource") DataSource radmDataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(radmDataSource);
        //sessionFactory.setMapperLocations(
          //      new PathMatchingResourcePatternResolver().getResources(RadmDataSourceConfig.MAPPER_LOCATION));
        return sessionFactory.getObject();
    }
}
